package com.vf.admin.client.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.dao.PhpUserDao;
import com.vf.admin.client.service.PhpUserService;
import com.vf.admin.miaodong.entity.PhpUser;
import com.vf.admin.utils.DatesUtil;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/10/21 17:19
 */
@Service
public class PhpUserServiceImpl implements PhpUserService {

    @Resource
    private PhpUserDao phpUserDao;


    @Override
    public void callBackWindowTick(Integer userId) {

        //判断是否存在门票 有不执行插入操作
        PhpUser phpUser = phpUserDao.getType(userId, 1);
        if (ObjectUtil.isEmpty(phpUser)) {
            //保存数据
            PhpUser phpUser1 = new PhpUser();
            phpUser1.setType(1);
            phpUser1.setUserid(userId);
            phpUserDao.save(phpUser1);
        }
    }

    @Override
    public void callBackVip(Integer userId, Integer vipPackageId) {

        //会员  1三天  2一个月  3六个月  7 12个月
        PhpUser phpUser = phpUserDao.getType(userId, 2);
        if (ObjectUtil.isEmpty(phpUser)) {

            String endVipTime = null;

            if (vipPackageId.equals(1)) {

                endVipTime = DatesUtil.addDay(new Date(), 3) + " 23:59:59";
            }
            if (vipPackageId.equals(2)) {

                Date date = DatesUtil.addMonth(new Date(), 1);
                endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
            }
            if (vipPackageId.equals(3)) {

                Date date = DatesUtil.addMonth(new Date(), 6);
                endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
            }
            if (vipPackageId.equals(7)) {

                Date date = DatesUtil.addMonth(new Date(), 12);
                endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
            }
            PhpUser phpUser1 = new PhpUser();
            phpUser1.setType(2);
            Date date1 = DatesUtil.strToDate(endVipTime, "yyyy-MM-dd HH:mm:ss");
            phpUser1.setEndtime(date1);
            phpUser1.setUserid(userId);
            phpUserDao.save(phpUser1);

        } else {

            String endVipTime = null;

            int result = phpUser.getEndtime().compareTo(new Date());
            if (result>0 ){

                if (vipPackageId.equals(1)) {

                    endVipTime = DatesUtil.addDay(phpUser.getEndtime(), 3) + " 23:59:59";

                }
                if (vipPackageId.equals(2)) {

                    Date date = DatesUtil.addMonth(phpUser.getEndtime(), 1);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }
                if (vipPackageId.equals(3)) {

                    Date date = DatesUtil.addMonth(phpUser.getEndtime(), 6);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }
                if (vipPackageId.equals(7)) {


                    Date date = DatesUtil.addMonth(phpUser.getEndtime(), 12);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }

            }else {
                if (vipPackageId.equals(1)) {

                    endVipTime = DatesUtil.addDay(new Date(), 3) + " 23:59:59";

                }
                if (vipPackageId.equals(2)) {

                    Date date = DatesUtil.addMonth(new Date(), 1);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }
                if (vipPackageId.equals(3)) {

                    Date date = DatesUtil.addMonth(new Date(), 6);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }
                if (vipPackageId.equals(7)) {


                    Date date = DatesUtil.addMonth(new Date(), 12);
                    endVipTime = DatesUtil.dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
                }
            }





            Date date1 = DatesUtil.strToDate(endVipTime, "yyyy-MM-dd HH:mm:ss");
            phpUser.setEndtime(date1);
            phpUserDao.save(phpUser);

        }

    }

    @Override
    public PhpUser getInfo(Integer id, Integer type) {

        PhpUser info = phpUserDao.getInfo(id, type);
        return info;
    }
}
